// Created by WXX on 2022/1/7 12:53
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

/**
 * 执行用时：8 ms, 在所有 C++ 提交中击败了98.68%的用户
 * 内存消耗：11.5 MB, 在所有 C++ 提交中击败了30.26%的用户
 */
class Solution {
public:
    int minTotalDistance(vector<vector<int>>& grid) {
        vector<int> xs, ys;
        for (int i = 0; i < grid.size(); i++)
            for (int j = 0; j < grid[0].size(); j++)
                if (grid[i][j]) {
                    xs.push_back(i);
                    ys.push_back(j);
                }
        return work(xs) + work(ys);
    }

    int work(vector<int> &q) {
        sort(q.begin(), q.end());
        int res = 0, n = q.size();
        for (int i = 0; i < n; i++) res += abs(q[i] - q[n / 2]);
        return res;
    }
};

int main() {


    return 0;
}
